package cn.fl.system.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.transform.SysDictionaryVOTransform;
import cn.fl.system.vo.SysDictionaryVO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共字典服务处理字典API服务
 * @author FUJINGRUN
 */
@Service
public class SysDictionaryServiceImpl implements ISysDictionaryService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysFlCompInfoServiceImpl.class);
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public Map<String, List<KeyValueVO>> getMultiKvList(String sysCd, String orgCd, List<String> codeGrpCdList)
			throws BaseException {
		Map<String, List<KeyValueVO>> result = new HashMap<>();
		try {
			result = this.sysDictionaryApiService.getMultiKvList(codeGrpCdList, sysCd, orgCd);
		} catch (Exception e) {
			BaseLogUtils.error(logger, "searchMultiGroupToMap", "获取数据字典失败!" + JSON.toJSONString(codeGrpCdList), CurrentThreadContext.getCurrentUserName());
		}finally {
			if(CollectionUtils.isMapEmpty(result)){
				result = new HashMap<>();
				for (String code : codeGrpCdList) {
					result.put(code, new ArrayList<KeyValueVO>());
				}
			}
		}
		return result;
	}

	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd, String sysCd, String orgCd) throws BaseException{
		return this.sysDictionaryApiService.getKvList(grpCd, sysCd, orgCd);
	}

	/**
	 * @MethodName getRoleisEncrypt
	 * @Description: 获取是否需要掩码判断
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2020/2/12 14:08
	 * @Version: V1.0.0
	 * @Param.
	 * @Return java.lang.String
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Override
	public String getRoleisEncrypt() throws BaseException{
		String rtn= BaseConstant.IS_YESNO_YES_STR;
		SysDictionaryDTO sysDictionaryDTO=this.sysDictionaryApiService.getCode(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"SYS0072",CurrentThreadContext.getCurrentRoleId()+"");
		if(null != sysDictionaryDTO && StringUtils.isNotEmpty(sysDictionaryDTO.getCodeNm())){
			rtn= sysDictionaryDTO.getCodeNm();
		}
		return rtn;
	}

	public List<SysDictionaryVO> searchGroup(String groupCd,String sysCd, String orgCd){
		List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDictionaryApiService.searchGroup(sysCd, orgCd, groupCd);
		return SysDictionaryVOTransform.toVOList(sysDictionaryDTOS);
	}

}
